package com.book.book.service;

import com.book.book.domain.User;
import com.book.book.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String userpassword) {
        // 优先使用 selectByUsernameAndPassword 进行精确匹配，因为这是登录
        User user = userMapper.selectByUsernameAndPassword(username, userpassword);
        return user; // 如果匹配成功，直接返回用户对象，否则返回 null
    }

    @Override
    public boolean register(User user) {
        // 注册前检查用户名是否已存在
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            return false; // 用户名已存在
        }
        // 设置默认角色，如果需要

        return userMapper.insert(user) > 0; // 插入成功返回 true
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
}